import requests
from bs4 import BeautifulSoup
import sys
from bookInfo import Book
from bookInfo import DownLoadInfo
import logging
sys.encoding = "utf8"
class PageFetch(object):
    host = "http://www.jb51.net/";   #域名+分类
    category = "books/";  #具体请求页
    def __init__(self,pageUrl):
        self.pageUrl = pageUrl;  #完整URL
        self.url = PageFetch.host+PageFetch.category + pageUrl;
    def __getPageContent(self):
        req = requests.get(self.url);
        if req.status_code == 200:
            req.encoding = "gb2312";
            strText = req.text;
            return strText;
        else:
            return "";
    def getPageContent(url):
        req = requests.get(url);
        if req.status_code == 200:
            req.encoding = "gb2312";
            strText = req.text;
            return strText;
        else:
            return "";
    def __getMaxPageNumAndUrl(self):
        fetchUrl = self.pageUrl;
        #获取分页地址 分页url 形如 list45_2.html 2为页号#
        maxPageNum = 0;
        maxLink = "";
        while maxLink == "":
            url = PageFetch.host+PageFetch.category +fetchUrl;
            reqContent = PageFetch.getPageContent(url)
            soup = BeautifulSoup (reqContent,"html.parser");
            for ul in soup.select(".plist"):
                print ("数据");
                print (ul);
                maxPageNum = ul.select("strong")[0].text;
                alink =  ul.select("a");
                if alink[-1]['href'] == "#":
                    maxLink = alink[1]['href'];
                else:
                    fetchUrl = alink[-1]['href'];
        return maxPageNum,maxLink;
    def __formatPage(self,pageNum):
        #格式化url  形如 list45_2.html#
        lineBeginSite = self.pageUrl.index("_")+1;
        docBeginSite = self.pageUrl.index(".");
        return self.pageUrl[:lineBeginSite]+str(pageNum+1)+self.pageUrl[docBeginSite:];
    def getBookPageList(self):
        #获取书籍每页的URL#
        shortPageList = [];
        maxPageNum,urlPattern = self.__getMaxPageNumAndUrl();
        for i in range(int(maxPageNum)):
            shortPageList.append(self.host +self.category+ self.__formatPage(i));
        return shortPageList;
    def getDownloadPage(url):
        downPage= [];
        reqContent = PageFetch.getPageContent(url);
        soup = BeautifulSoup (reqContent,"html.parser");
        for a in soup.select(".cur-cat-list .btn-dl"):
            downPage.append(PageFetch.host+a['href']);
        return downPage;
    def getBookInfo(url):
        logging.info("获取书籍信息url:%s" % url);
        reqContent = PageFetch.getPageContent(url);
        soup = BeautifulSoup (reqContent,"html.parser");
        mainInfo = (soup.select("#soft-intro"))[0].text.replace("截图：","").replace("'","");
        title = (soup.select("dl dt h1"))[0].text.replace("'","");
        book = Book(mainInfo,url,title);
        for ul in soup.select(".ul_Address"):
            for li in ul.select("li"):
                downLoadInfo = DownLoadInfo(li.select("a")[0]['href'],li.select("a")[0].text);
                book.addDownLoadUrl(downLoadInfo);
        return book;
if __name__ == '__main__':
    p = PageFetch("list152_1.html");
    shortPageList = p.getBookPageList();
    downPage= [];
    for page in shortPageList:
        downLoadPage =  PageFetch.getDownloadPage(page);
        downPage = downPage+downLoadPage;
    print ("================汇总如下===============================");
    for bookDownLoadPage in downPage:
        book  = PageFetch.getBookInfo(bookDownLoadPage);
        print (book.bookName+":%s" %  book.downLoadUrl);
        for d in book.downLoadInfos:
            print ("%s - %s" % (d.downUrl,d.downName));
    # p = PageFetch("list977_1.html");
    # p = p.getMaxPageNumAndUrl();
    # print (p);
